package com.jwdemo
{
	import com.jwdemo.Explosion;
	import com.jwdemo.GameObjectManager;
	import com.jwdemo.InfGameObject;
	import com.jwdemo.TextureAnim;

	public class Bullet implements InfGameObject
	{
			
		private var position:Vector4 ;
		private var _owner:InfMoveableObject ;
		private var _time:Number ;
		private var _scale:Number ;
		private var _radius:Number ;
		private var _animation:TextureAnim ;
		private var _duration:Number ;
		private var _angle:Number ;
		private var _initVel:Number ;
		private var _damage:Number ;
		private var _strength:Number ;

		private var _x:Number ;
		private var _y:Number ;
		private var _z:Number ;

		private var _xvel:Number ;
		private var _yvel:Number ;
		private var _ftime:Boolean ;

		public var className:String = "Bullet" ;

		// TODO change type for 'owner' to InfMoveableObject or similar. (Allow Enemy and Player Objects to use this)
		
		public function Bullet(owner:Enemy, damage:Number, velocity:Number, angle:Number, duration:Number, animation:TextureAnim, scale:Number, radius:Number)
		{
			var	cos:Number			=	Math.cos(angle*Math.PI/180) ;
			var sin:Number			=	Math.sin(angle*Math.PI/180) ;

			this._owner				=	owner ;
			this.position			=	owner.getVectorPosition() ;

			this._damage			=	damage ;
			this._initVel			=	velocity ;
			this._angle				=	angle ;
			this._duration			=	duration ;
			this._animation			=	animation ;
			this._scale				=	scale  ;
			this._radius			=	radius ;

			
			var owneroffset:Object	=	owner.getFiringOffset(-1) ;
			var ownerpos:Object 	=	owner.getPosition() ;
			var x:Number			=	ownerpos._x, y:Number = ownerpos._y, z:Number = ownerpos._z ;
			var ox:Number			=	owneroffset.x, oy:Number = owneroffset.y, oz:Number = owneroffset.z ;
			
			
			
			var	tox:Number			=	ox*cos - oy*sin ;
			var	toy:Number			=	ox*sin + oy*cos ;
			
			
			this._x					=	x +  tox;
			this._y					=	y +  toy ;
			this._z					=	z +  oz  ;
			this._yvel				=	-velocity * cos ;
			this._xvel				=	velocity * sin ;
			
			this._time				=	-1 ;
			this._strength			=	100 ; //BULLET_DEFAULT_STRENGTH ;
			this._ftime				=	false ;
			
			if (this._animation) {
				this._animation.play(true,false) ;
			}

		}
		
		public function update(dt:Number) : void
		{
			var	dx:Number	=	dt*this._xvel, dy:Number = dt*this._yvel ;
			
			// Don't update position until we have rendered at 
			// least once. Otherwise we don't see bullet's leave their nozzles.
			
			if (_ftime)
			{
				
				this._time		=	this._time + dt ;
				this._x 		=	this._x + dx ;
				this._y			= 	this._y + dy ;
			} else {
				_ftime		=	true ;
			}
			
			if (this._animation)
			{
				this._animation.update(dt) ;
			}
			
			// TODO add collision checks here!
			
		}

		public function render(rHelper:RenderHelper) : void
		{
			
			if (this._animation)
			{
				var texInfo:Object = this._animation.getRenderTexture() ;
				rHelper.drawSheet(texInfo.sheet,this._x,this._y,texInfo.frame,this._angle,0.5,0.5) ;

			}
			//rHelper.drawCircle(this._x, this._y,2) ;
			
		}
		
		public function applyDamage(damage:Number) : void
		{
			explode() ;
			
		}

		public function isAlive():Boolean 
		{
			return !isFree() ;
		}

		public function isFree():Boolean 
		{
			return this._time > this._duration ;
		}
		
		
		public function explode() : void
		{
			GameObjectManager.Explode(this._x,this._y) ;
		}



	
	}
}